﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using UnityEditor;
using UnityEngine;

public class NewBehaviourScript : EditorWindow
{
    [MenuItem("Tools/Editor/编译运行 &1", false, 11)]
    static void SVNCompileScript()
    {
        Assembly[] ass = AppDomain.CurrentDomain.GetAssemblies();
        Assembly as1 = null;
        Assembly as2 = null;
        for (int i = 0; i < ass.Length; i++)
        {
            if (ass[i].FullName.IndexOf("Unity.2D.Animation.Editor") >= 0)
                as1 = ass[i];
            if (ass[i].FullName.IndexOf("Unity.2D.Sprite.Editor") >= 0)
                as2 = ass[i];
        }
        Type type_SpriteEditorWindow = as2.GetType("UnityEditor.U2D.Sprites.SpriteEditorWindow");
        Type type_SkinningModule = as1.GetType("UnityEditor.U2D.Animation.SkinningModule");
        Type type_GenerateGeometryTool = as1.GetType("UnityEditor.U2D.Animation.GenerateGeometryTool");
        Type type_GenerateGeometryPanel = as1.GetType("UnityEditor.U2D.Animation.GenerateGeometryPanel");

        FieldInfo field_SpriteEditor_CurrentModule = type_SpriteEditorWindow.GetField("m_CurrentModule", BindingFlags.Instance | BindingFlags.NonPublic);
        PropertyInfo prop_SkinningModule_CurrentTool = type_SkinningModule.GetProperty("currentTool", BindingFlags.Instance | BindingFlags.NonPublic);
        FieldInfo field_GenerateGeometryPanel = type_GenerateGeometryTool.GetField("m_GenerateGeometryPanel", BindingFlags.Instance | BindingFlags.NonPublic);
        MethodInfo meth_Geometry_GenerateGeometryAll = type_GenerateGeometryPanel.GetMethod("GenerateGeometryAll", BindingFlags.Instance | BindingFlags.Public);
        MethodInfo s_meth_SkinningModule_ApplyRevert = type_SpriteEditorWindow.GetMethod("DoApply", BindingFlags.Instance | BindingFlags.NonPublic);
        MethodInfo s_meth_SkinningModule_SetupModule = type_SpriteEditorWindow.GetMethod("SetupModule", BindingFlags.Instance | BindingFlags.NonPublic);

        EditorWindow sWindow = EditorWindow.GetWindow(type_SpriteEditorWindow);
        object s_CurrentModule_Obj = field_SpriteEditor_CurrentModule.GetValue(sWindow);
        object m_GenerateGeometryTool_Obj = prop_SkinningModule_CurrentTool.GetValue(s_CurrentModule_Obj);
        object m_GenerateGeometryPanel_Obj = field_GenerateGeometryPanel.GetValue(m_GenerateGeometryTool_Obj);
        meth_Geometry_GenerateGeometryAll.Invoke(m_GenerateGeometryPanel_Obj, null);

        s_meth_SkinningModule_ApplyRevert.Invoke(sWindow, new object[] { });
        s_meth_SkinningModule_SetupModule.Invoke(sWindow, new object[] { 3 });
    }

}
